#include "Waveform_QT.h"

#include <QString>
#include <QFile>
#include <QDataStream>
#include <QDebug>


using namespace std;

Waveform_QT::Waveform_QT():
mWaveform(),
mFilteredWaveform(),
mAnnotation()
{
}

Waveform_QT::~Waveform_QT()
{
}

bool Waveform_QT::Load(const QString& dataPath, const QString& annotationPath) {
	QFile f(dataPath);
	if (!f.exists() || !f.open(QFile::ReadOnly))
		return false;


	QDataStream stream(&f);
	short value = 0;
	int number = f.size() / sizeof(short);
	short* values = new short[number];
	memset(values, 0, sizeof(short) * number);
	stream.readRawData((char*)values, number * sizeof(short));

	mSingleWaveform.clear();
	mSingleWaveform.reserve(number);
	for (int n = 0; n < number; ++n){
		mSingleWaveform.push_back(values[n]);
	}

	delete[] values;
	f.close();
	return true;
}

QVector<short> const Waveform_QT::getWaveform() {
    return mSingleWaveform;
}

#ifdef _DEBUG
void Waveform_QT::limitDataLength(int len /*= 7000*/)
{
	for (int n = mSingleWaveform.size() - 1; n >= len; --n){
		mSingleWaveform.pop_back();
	}
}

void Waveform_QT::initWave(Waveform& wave)
{
// 	for (int ch = 0; ch < ECG12_CHANNELS; ++ch)
// 	{
// 		mWaveform[ch].clear();
// 		std::vector<int16_t> vWaveValues = wave.getWaveform(ch);
// 		mWaveform[ch] = std_qt::ConvertVector(vWaveValues);
// 
// 		mFilteredWaveform[ch].clear();
// 		std::vector<double> vWaveFilteredValues = wave.getFilteredWaveform(ch);
// 		mFilteredWaveform[ch] = std_qt::ConvertVector(vWaveFilteredValues);
// 
// 		mAnnotation[ch].clear();
// 		std::map<std::string, std::vector<uint32_t> > mapAnnotations = wave.getAnnotation(ch);
// 		QMap<QString, QVector<int>> mapAnnotations_QT;
// 		for (std::map<std::string, std::vector<uint32_t> >::iterator iter = mapAnnotations.begin();
// 			iter != mapAnnotations.end(); ++iter)
// 		{
// 			QVector<int> vAnnotations;
// 			for (int n = 0; n < iter->second.size(); ++n)
// 			{
// 				vAnnotations.push_back(iter->second.at(n));
// 			}
// 			mapAnnotations_QT.insert(QString::fromStdString(iter->first), vAnnotations);
// 		}
// 		mAnnotation[ch] = mapAnnotations_QT;
// 	}
}

#endif